//
//  ViewController.m
//  YDTestDrive
//
//  Created by ios on 2017/2/7.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "ViewController.h"
#import "YDMainTabBarController.h"
#import "AFNetworking.h"
#import "NSString+Mobile.h"
#import "UIButton+XZ.h"

@interface ViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *loginBGView; //登录背景View
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField; //手机号码输入框
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeTextField; //验证码输入框
@property (weak, nonatomic) IBOutlet UIButton *getCodeButton; //获取验证码按钮
@property (weak, nonatomic) IBOutlet UIButton *loginButton; //登录按钮
@property (weak, nonatomic) IBOutlet UIImageView *phoneInputBGView; //手机号码输入框背景图片
@property (weak, nonatomic) IBOutlet UIButton *timeButton; //倒计时按钮

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self setupView];
    
}

- (void)setupView
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewClick)];
    self.loginBGView.userInteractionEnabled = YES;
    [self.loginBGView addGestureRecognizer:tap];
    
    self.phoneTextField.delegate = self;
    self.verifyCodeTextField.delegate = self;
    
    self.timeButton.TimeEndBlock = ^{
        self.timeButton.hidden = YES;
        self.getCodeButton.hidden = NO;
    };
}

- (void)bgViewClick
{
    [self.view endEditing:YES];
}

#pragma mark 获取验证码按钮点击
- (IBAction)getCodeButtonClick {

    if (![self.phoneTextField.text isMobilePhone]) {
        [MBProgressHUD showTips:@"请输入正确的手机号码" toView:self.view];
        self.phoneInputBGView.image = [UIImage imageNamed:@"login-_input_error"];
        return;
    } else {
        self.phoneInputBGView.image = [UIImage imageNamed:@"login-_input_correct"];
        [self.verifyCodeTextField becomeFirstResponder];
    }
    
    NSDictionary *dic = @{@"phone" : self.phoneTextField.text,
                          @"type" : @"get",
                          @"source" : @"trial"};
    
    [self getDataFormNetworkWithParameters:dic type:1];
}

#pragma mark 登录按钮点击
- (IBAction)loginButtonClick {
    
    NSDictionary *dic = @{@"phone" : self.phoneTextField.text,
                          @"idCode" : self.verifyCodeTextField.text,
                          @"type" : @"valid",
                          @"source" : @"trial"};
    
    [self getDataFormNetworkWithParameters:dic type:2];

}

#pragma mark 获取验证码接口 type: "1" 获取验证码 "2" 验证验证码、登录
- (void)getDataFormNetworkWithParameters:(NSDictionary *)parameters type:(NSInteger)type
{
    
    NSString *URL = [NSString stringWithFormat:@"%@", kGetCodeAndLogin];
    
    [YDDataService POST:parameters url:URL block:^(id result) {
        
        NSString *statusCode = [result valueForKey:@"code"];
        
        if ([statusCode isEqualToString:@"S_OK"])
        {
            
            if (type == 1) {
                [MBProgressHUD showTips:@"验证码发送成功" toView:self.view];
                self.getCodeButton.hidden = YES;
                self.timeButton.hidden = NO;
                [self.timeButton startCountdown];
                
            } else {
                NSString *sid = [[result valueForKey:@"var"] valueForKey:@"sid"];
                setNSUser(sid, kAPP_SID);
                
                YDMainTabBarController *mainTabBarController = [[YDMainTabBarController alloc] init];
                [self presentViewController:mainTabBarController animated:YES completion:^{
                    self.view.window.rootViewController = mainTabBarController;
                }];
            }
        }
        else
        {
            [self loginErrorWithCode:statusCode];
        }
        
    } failBlock:^(id error) {
        [MBProgressHUD showTips:[NSString stringWithFormat:@"%@",error] toView:self.view];
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //当前文字
    NSMutableString *currentString = [NSMutableString stringWithString:textField.text];
    
    if (string.length > 0) {
        //输入文字
        [currentString insertString:string atIndex:range.location];
    } else {
        //删除文字
        [currentString deleteCharactersInRange:range];
    }
    
    if (textField == self.phoneTextField) {
        if (string.length == 0) return YES;
        
        if (currentString.length > 11) {
            return NO;
        }
    } else if (textField == self.verifyCodeTextField) {
        if (currentString.length > 6) {
            return NO;
        }
    }
    
    return YES;
}

- (void)loginErrorWithCode:(NSString *)errorCode
{

    if ([errorCode isEqualToString:@"VERIFYCODE_G_FAL_0X003"])
    {
        [MBProgressHUD showTips:@"手机号码为空" toView:self.view];
    }
    else if ([errorCode isEqualToString:@"VERIFYCODE_V_FAL_0X001"])
    {
        [MBProgressHUD showTips:@"验证码错误" toView:self.view];
    }
    else if ([errorCode isEqualToString:@"VERIFYCODE_G_FAL_0X001"])
    {
        [MBProgressHUD showTips:@"请求方式错误" toView:self.view];
    }
    else if ([errorCode isEqualToString:@"MEMBER_Q_FAL_0X007"])
    {
        [MBProgressHUD showTips:@"成员数据异常" toView:self.view];
    }
    else if ([errorCode isEqualToString:@"MEMBER_Q_FAL_0X003"])
    {
        [MBProgressHUD showTips:@"成员状态有异常" toView:self.view];
    }
    else if ([errorCode isEqualToString:@"MEMBER_Q_FAL_0X005"])
    {
        [MBProgressHUD showTips:@"不具备登录设备的权限" toView:self.view];
    }
    else if ([errorCode isEqualToString:@"MEMBER_Q_FAL_0X001"])
    {
        [MBProgressHUD showTips:@"未找到该成员" toView:self.view];
    }
    else
    {
        [MBProgressHUD showTips:errorCode toView:self.view];
    }
}

@end
